package com.tuanzi.loan.business.service.wechat.impl;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.tuanzi.loan.business.entity.wechat.LoanWechatCustomerMessage;
import com.tuanzi.loan.business.repository.wechat.WechatCustomerMessageRepository;
import com.tuanzi.loan.business.service.wechat.WechatCustomerMessageService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.repository.JPAAuditRepository;
import com.tuanzi.loan.core.service.BaseAuditServiceImpl;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.wechat.WechatCustomerMessageManageRequest;
import com.tuanzi.loan.vo.request.wechat.WechatCustomerMessageRequest;
import com.tuanzi.loan.vo.response.wechat.WechatCustomerMessageResponse;
import com.tuanzi.server.loan_base.constant.weixin.LoanWechatCustomerMessageMsgType;
import com.tuanzi.server.loan_base.constant.weixin.LoanWechatCustomerMessageStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: chenweijun
 * @Date: 2018/9/27 17:42
 * @Description:
 */
@Debug
@Slf4j
@Service
@Transactional
@Retryable(value = { RetryException.class })
public class WechatCustomerMessageServiceImpl extends BaseAuditServiceImpl<LoanWechatCustomerMessage, Long> implements WechatCustomerMessageService{

    @Autowired
    private WechatCustomerMessageRepository wechatCustomerMessageRepository;

    @Override
    protected JPAAuditRepository<LoanWechatCustomerMessage, Long> getRepository() {
        return wechatCustomerMessageRepository;
    }


    @Override
    public Page<WechatCustomerMessageResponse> list(PageSearcher<WechatCustomerMessageRequest> pageSearcher) {
        return wechatCustomerMessageRepository.list(pageSearcher.getSearchCondition(), pageSearcher.getPageable());
    }

    @Override
    public LoanWechatCustomerMessage saveLoanWechatCustomerMessage(WechatCustomerMessageManageRequest request) {

        String msgContent = "";
        if(request.getMsgType().equals(LoanWechatCustomerMessageMsgType.TEXT.getValue())){

            JsonObject textJson = new JsonObject();
            textJson.addProperty("content", request.getContent());

            msgContent = textJson.toString();

        }else if(request.getMsgType().equals(LoanWechatCustomerMessageMsgType.IMAGE.getValue())){

            JsonObject imageJson = new JsonObject();
            imageJson.addProperty("media_id", request.getMediaId());
            imageJson.addProperty("picurl", request.getPicUrl() );

            msgContent = imageJson.toString();

        }else if(request.getMsgType().equals(LoanWechatCustomerMessageMsgType.NEWS.getValue())){

            JsonObject newsJson = new JsonObject();
            newsJson.addProperty("title", request.getTitle());
            newsJson.addProperty("description", request.getDescription());
            newsJson.addProperty("url", request.getUrl());
            newsJson.addProperty("picurl", request.getPicUrl());

            JsonArray newsJsonArray = new JsonArray();
            newsJsonArray.add(newsJson);

            msgContent = newsJsonArray.toString();


        }else if(request.getMsgType().equals(LoanWechatCustomerMessageMsgType.NO_PICTURE_NEWS.getValue())){

            JsonObject newsJson = new JsonObject();
            newsJson.addProperty("title", request.getTitle());
            newsJson.addProperty("description", request.getDescription());
            newsJson.addProperty("url", request.getUrl());

            JsonArray newsJsonArray = new JsonArray();
            newsJsonArray.add(newsJson);

            msgContent = newsJsonArray.toString();

        }
        LoanWechatCustomerMessage message = null;
        if (request.getId() != null){
            message = wechatCustomerMessageRepository.findOne(request.getId());
        }
        else {
            message = new LoanWechatCustomerMessage();
        }
        message.setName(request.getName());
        message.setEffectiveStartTime(request.getEffectiveStartTime());
        message.setEffectiveEndTime(request.getEffectiveEndTime());
        message.setProductId(request.getProductId());
        message.setStatus(LoanWechatCustomerMessageStatus.ONLINE.ordinal());
        message.setTriggerBehavior(request.getTriggerBehavior());
        message.setTriggerTime(request.getTriggerTime());
        message.setMsgType(request.getMsgType());
        message.setMsgContent(msgContent);

        return wechatCustomerMessageRepository.save(message);
    }

}
